package com.wlstock.funi.utils;

import android.content.Context;
import android.support.annotation.NonNull;

import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.dao.AIAccountDao;
import com.wlstock.funi.dao.PositionDao;
import com.wlstock.funi.dao.SelfFundSetDao;
import com.wlstock.funi.dao.TradeHisDao;
import com.wlstock.funi.model.AIAccountsModel;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundModel;
import com.wlstock.funi.model.SelfFundSetModel;
import com.wlstock.funi.runnable.OrderListener;
import com.wlstock.funi.runnable.OrderSale;

import java.math.BigDecimal;

public class SellHelper implements OrderListener {
	
	private Context mContext;
	public static final int SELL_NOW = 1;
	private static SellHelper instance;

	private SellHelper(Context mContext) {
		this.mContext = mContext;
	}

	public static SellHelper getInstance(Context mContex) {
		if (instance == null) {
			synchronized (SellHelper.class) {
				if (instance == null) {
					instance = new SellHelper(mContex);
				}
			}
		}
		return instance;
	}


	public void orderAISaile(SelfFundModel tmpSelfFundModel, AIAccountsModel aiStock, String offset) {
		if (aiStock.getStkQty()!=0) {
			//达到止盈条件
			SelfFundSetDao.getInstance(mContext).updatePendState(tmpSelfFundModel.getStockNo(), SelfFundSetModel.DONE, OperateModel.TradeType_SELL);
			NotiUtils.showMyAccontMode(mContext,aiStock,offset);
			TradeHisDao.getInstance(mContext).addSAILEModel(aiStock);
			//删除持仓纪录
			AIAccountDao.getInstance(mContext).delete(aiStock);
			//更新仓位信息
			PositionDao.getInstance(mContext).updateCurPosition(OperateModel.TradeType_SELL, aiStock.getStkQty(), Float.valueOf(aiStock.getCurPrice()));
		}
	}

	public void orderSaile(SelfFundModel tmpSelfFundModel, MyAccountStocks ownStock, String speakTxt) {
		if (FundApplication.isLogSuccess && ownStock.getStkQty()!=0) {

			OrderSale orderSale = new OrderSale( ownStock.getStkCode(), ownStock.getStkName(), getPrice(tmpSelfFundModel.getCurPrice()), ownStock.getStkQty());
			orderSale.saile(this);
//			ownStock.setStkQty(0);
//			MyAccountStockDao.getInstance(LatestPriceService.this).createOrUpdateStock(ownStock);
			//语音播报
			FundApplication.getInstance().speak(speakTxt);

		}
	}

	@NonNull
	public float getPrice(float price) {
		float buyPercent = GlobalIndexHelper.getInstance(mContext).getBuyPercent();
		float tmp = price;
		if (buyPercent!=-1){
			if (buyPercent < GlobalIndexHelper.LOWEST_ENV_FEEL){
				tmp = price;
			}else if (GlobalIndexHelper.LOWEST_ENV_FEEL<=buyPercent && buyPercent <=GlobalIndexHelper.HIGHEST_ENV_FEEL){
				tmp = price * (1 + (buyPercent-GlobalIndexHelper.LOWEST_ENV_FEEL)/100);
			}else {
				tmp = price * (1 + (buyPercent-GlobalIndexHelper.HIGHEST_ENV_FEEL)/100);
			}
		}
		BigDecimal de = new BigDecimal(tmp);
		//保留2位小数
		return de.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();
	}


	public boolean isNeedRebackSaile(float ownFloatDis, float hPoriftRate) {
		if (hPoriftRate>0.1){
			return ownFloatDis < hPoriftRate*2/3;
		}else if (hPoriftRate >0.05){
			return  ownFloatDis < hPoriftRate*2/3;
		}else if (hPoriftRate >0.02){
			return  ownFloatDis < hPoriftRate-0.015;
		}
		return false;
	}

	/**
	 * 是否止损
	 * @param curPrice 现在价格
	 * @param targetPrice  目标价格(针对移动止损则是最高价，对于其他判断形式，也可能是昨日最低价格，也可能是买入价格)
	 * @param offset 偏移
	 * @return
	 */
	public boolean isZX(float curPrice, float targetPrice,float offset){
		return (curPrice-targetPrice)/targetPrice < offset;
	}
	
	/**
	 * 是否止损
	 * @param curPrice 现在价格
	 * @param targetPrice  目标价格(针对移动止损则是最高价，对于其他判断形式，也可能是昨日最低价格，也可能是买入价格)
	 * @param offset 偏移
	 * @param inZXTimeRange 是否在某个时间段内进行止损
	 * @return
	 */
	public boolean isZXInRange(float curPrice, float targetPrice,float offset, boolean inZXTimeRange){
		if (inZXTimeRange) {
			return isZX(curPrice, targetPrice, offset);
		}
		return false;
	}
	
	/**
	 * 判断止损是马上止损还是延时止损
	 * @param mContext
	 * @return
	 */
	public static int getLoseType(Context mContext) {
		// TODO Auto-generated method stub
		//查当前上涨跟下跌的比例，查当前所有人的持仓比例，查询今天的基本推荐持仓
		return SELL_NOW;
	}

	@Override
	public void orderSuccess(int tradeType, String stkcode, String stkName, float price, long qty) {
		if (tradeType == OperateModel.TradeType_SELL){

		}
	}

	@Override
	public void orderFaile(int tradeType, String stkcode, String stkName, float price, long qty, String info) {
		if (tradeType == OperateModel.TradeType_SELL){

		}
	}
}
